package view;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import robocopy.actions.MainAction;

import com.thoughtworks.xstream.XStream;

import model.Usuario;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;

public class CadastroUsuario {

	private JFrame frmRobocopyCadastro;
	private JTextField txtCadNome;
	private JTextField txtCadIP;
	private JTextField txtCadPorta;

			public static void run() {
				try {
					CadastroUsuario window = new CadastroUsuario();
					window.frmRobocopyCadastro.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

	/**
	 * Create the application.
	 */
	public CadastroUsuario() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmRobocopyCadastro = new JFrame();
		frmRobocopyCadastro.setResizable(false);
		frmRobocopyCadastro.setTitle("RoboCopy - Cadastro de Usuário");
		frmRobocopyCadastro.setBounds(100, 100, 349, 159);
		frmRobocopyCadastro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmRobocopyCadastro.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("Nome:");
		label.setBounds(22, 41, 70, 15);
		frmRobocopyCadastro.getContentPane().add(label);
		
		txtCadNome = new JTextField();
		txtCadNome.setColumns(10);
		txtCadNome.setBounds(75, 39, 220, 19);
		frmRobocopyCadastro.getContentPane().add(txtCadNome);
		
		JLabel label_1 = new JLabel("Sua identificação no RoboCopy:");
		label_1.setBounds(12, 12, 243, 15);
		frmRobocopyCadastro.getContentPane().add(label_1);
		
		txtCadIP = new JTextField();
		txtCadIP.setColumns(10);
		txtCadIP.setBounds(75, 68, 114, 19);
		frmRobocopyCadastro.getContentPane().add(txtCadIP);
		
		txtCadPorta = new JTextField();
		txtCadPorta.setColumns(10);
		txtCadPorta.setBounds(75, 99, 114, 19);
		frmRobocopyCadastro.getContentPane().add(txtCadPorta);
		
		JLabel label_2 = new JLabel("Porta:");
		label_2.setBounds(22, 101, 70, 15);
		frmRobocopyCadastro.getContentPane().add(label_2);
		
		JLabel label_3 = new JLabel("IP:");
		label_3.setBounds(22, 70, 70, 15);
		frmRobocopyCadastro.getContentPane().add(label_3);
		
		JButton button = new JButton("Salvar");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Usuario user = new Usuario();
				user.setIp(txtCadIP.getText());
				user.setPorta(Integer.parseInt(txtCadPorta.getText()));
				user.setNome(txtCadNome.getText());
				user.setListaParceiros(null);
				try{
					XStream xs= new XStream();
					xs.alias("Usuario", Usuario.class);
					File arq = new File(".usuario.xml");
					arq.createNewFile();
					FileWriter writer = new FileWriter(arq,true);
					xs.toXML(user, writer);
					writer.close();	
					MainAction.setUser(user);
				}catch (Exception ex) {
					// TODO: handle exception
					System.out.println(ex.toString());
				}
				MainForm main = new MainForm();
				main.run();
				JOptionPane.showMessageDialog(frmRobocopyCadastro, "Usuário Cadastrado!");
				frmRobocopyCadastro.dispose();
			}
		});
		button.setBounds(218, 96, 117, 25);
		frmRobocopyCadastro.getContentPane().add(button);
	}
}
